/*
 * MrSudokuView.java
 */
package mrsudoku;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 * The application's main frame.
 */
public class MrSudokuView extends FrameView
{

    /**
     * 
     * @param app 
     */
    public MrSudokuView(SingleFrameApplication app)
    {
        super(app);
        initComponents();
        // ab hier code einfügen
        editMenu.setEnabled(false);
        rMap = MrSudokuApp.getApplication().getContext().getResourceMap(MrSudokuView.class);

        mainPanel.setLayout(new GridLayout(3, 3));


        mainPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);




        sudokuData = new MrSudokuDigitBox[9][9];

        for (int i = 0; i < 9; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                MrSudokuDigitBox digiBox = new MrSudokuDigitBox(i, j);
                
                sudokuData[i][j] = digiBox;

            }
            // System.out.println();

        }

        int n = 0;
        int m = 0;

        for (int i = 0; i < 9; ++i)
        {
            JPanel panel = new JPanel(new GridLayout(3, 3));
            panel.setBorder(new LineBorder(Color.DARK_GRAY, 2, true));
            mainPanel.add(panel);


            for (int j = 0; j < 3; ++j)
            {
                for (int k = 0; k < 3; ++k)
                {
                    panel.add(sudokuData[j + m][k + n]);
                   // System.out.print("[" + (j + n) + " , " + (k + m) + "] ");
                }
                System.out.println();

            }
            if ((i +1) % 3 == 0) m += 3;
            if (m > 6)
            {
                m = 0;
            }
            System.out.println();
            n += 3;
            if (n > 6)
            {
                n = 0;
            }

        }


        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++)
        {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener()
        {

            public void propertyChange(java.beans.PropertyChangeEvent evt)
            {

                System.out.println("form");

                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName))
                {
                    if (!busyIconTimer.isRunning())
                    {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName))
                {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName))
                {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName))
                {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    /**
     * 
     */
    @Action
    public void showAboutBox()
    {

        if (aboutBox == null)
        {
            JFrame mainFrame = MrSudokuApp.getApplication().getMainFrame();
            aboutBox = new MrSudokuAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);

        }
        MrSudokuApp.getApplication().show(aboutBox);
    }

    /**
     *
     */
    @Action
    public void jMenuItemNeu()
    {
        for (int i = 0; i < sudokuData.length; i++)
        {
            for (int j = 0; j < sudokuData.length; j++)
            {
                sudokuData[i][j].setText("0");
                MrSudokuApp.getApplication().setArrEditable(true, i, j);
            }
        }
        setDefaultBackground();
        setDefaultForeground();
        setAllEnabledAndEditable(true);
        MrSudokuApp.getApplication().setGameMode(false);
        statusMessageLabel.setText("");

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveActiveMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        errorMenuItem = new javax.swing.JMenuItem();
        hintMenuItem = new javax.swing.JMenuItem();
        loesenMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        manualMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        gameModeBtn = new javax.swing.JToggleButton();

        mainPanel.setMaximumSize(new java.awt.Dimension(960, 960));
        mainPanel.setMinimumSize(new java.awt.Dimension(960, 960));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(960, 960));
        mainPanel.setLayout(new java.awt.GridLayout(1, 0));

        menuBar.setMaximumSize(new java.awt.Dimension(133, 960));
        menuBar.setName("menuBar"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(mrsudoku.MrSudokuApp.class).getContext().getActionMap(MrSudokuView.class, this);
        fileMenu.setAction(actionMap.get("openSudoku")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mrsudoku.MrSudokuApp.class).getContext().getResourceMap(MrSudokuView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        fileMenu.setMaximumSize(new java.awt.Dimension(35, 960));
        fileMenu.setName("fileMenu"); // NOI18N

        newMenuItem.setAction(actionMap.get("newSudoku")); // NOI18N
        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setIcon(resourceMap.getIcon("newMenuItem.icon")); // NOI18N
        newMenuItem.setText(resourceMap.getString("newMenuItem.text")); // NOI18N
        newMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        newMenuItem.setName("newMenuItem"); // NOI18N
        fileMenu.add(newMenuItem);

        openMenuItem.setAction(actionMap.get("openSudoku")); // NOI18N
        openMenuItem.setIcon(resourceMap.getIcon("openMenuItem.icon")); // NOI18N
        openMenuItem.setText(resourceMap.getString("openMenuItem.text")); // NOI18N
        openMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        openMenuItem.setMaximumSize(new java.awt.Dimension(960, 960));
        openMenuItem.setName("openMenuItem"); // NOI18N
        fileMenu.add(openMenuItem);

        saveActiveMenuItem.setAction(actionMap.get("saveActive")); // NOI18N
        saveActiveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveActiveMenuItem.setIcon(resourceMap.getIcon("saveActiveMenuItem.icon")); // NOI18N
        saveActiveMenuItem.setText(resourceMap.getString("saveActiveMenuItem.text")); // NOI18N
        saveActiveMenuItem.setName("saveActiveMenuItem"); // NOI18N
        fileMenu.add(saveActiveMenuItem);

        saveMenuItem.setAction(actionMap.get("saveSudoku")); // NOI18N
        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        saveMenuItem.setIcon(resourceMap.getIcon("saveMenuItem.icon")); // NOI18N
        saveMenuItem.setText(resourceMap.getString("saveMenuItem.text")); // NOI18N
        saveMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        saveMenuItem.setMaximumSize(new java.awt.Dimension(960, 960));
        saveMenuItem.setName("saveMenuItem"); // NOI18N
        fileMenu.add(saveMenuItem);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setIcon(resourceMap.getIcon("exitMenuItem.icon")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setToolTipText(resourceMap.getString("exitMenuItem.toolTipText")); // NOI18N
        exitMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        exitMenuItem.setMaximumSize(new java.awt.Dimension(960, 960));
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setAction(actionMap.get("inputHelper")); // NOI18N
        editMenu.setText(resourceMap.getString("editMenu.text")); // NOI18N
        editMenu.setMaximumSize(new java.awt.Dimension(65, 960));
        editMenu.setName("editMenu"); // NOI18N

        errorMenuItem.setAction(actionMap.get("jMenuItemFehlerAnzeigen")); // NOI18N
        errorMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        errorMenuItem.setIcon(resourceMap.getIcon("errorMenuItem.icon")); // NOI18N
        errorMenuItem.setText(resourceMap.getString("errorMenuItem.text")); // NOI18N
        errorMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        errorMenuItem.setName("errorMenuItem"); // NOI18N
        editMenu.add(errorMenuItem);

        hintMenuItem.setAction(actionMap.get("jMenuItemLoesungshinweis")); // NOI18N
        hintMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        hintMenuItem.setIcon(resourceMap.getIcon("hintMenuItem.icon")); // NOI18N
        hintMenuItem.setText(resourceMap.getString("hintMenuItem.text")); // NOI18N
        hintMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        hintMenuItem.setName("hintMenuItem"); // NOI18N
        editMenu.add(hintMenuItem);

        loesenMenuItem.setAction(actionMap.get("solveSudoku")); // NOI18N
        loesenMenuItem.setIcon(resourceMap.getIcon("loesenMenuItem.icon")); // NOI18N
        loesenMenuItem.setText(resourceMap.getString("loesenMenuItem.text")); // NOI18N
        loesenMenuItem.setName("loesenMenuItem"); // NOI18N
        editMenu.add(loesenMenuItem);

        menuBar.add(editMenu);

        helpMenu.setAction(actionMap.get("showAboutBox")); // NOI18N
        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setMaximumSize(new java.awt.Dimension(33, 960));
        helpMenu.setName("helpMenu"); // NOI18N

        manualMenuItem.setAction(actionMap.get("manualSudoku")); // NOI18N
        manualMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F9, 0));
        manualMenuItem.setIcon(resourceMap.getIcon("manualMenuItem.icon")); // NOI18N
        manualMenuItem.setText(resourceMap.getString("manualMenuItem.text")); // NOI18N
        manualMenuItem.setToolTipText(resourceMap.getString("manualMenuItem.toolTipText")); // NOI18N
        manualMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        manualMenuItem.setName("manualMenuItem"); // NOI18N
        helpMenu.add(manualMenuItem);

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setIcon(resourceMap.getIcon("aboutMenuItem.icon")); // NOI18N
        aboutMenuItem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        aboutMenuItem.setMaximumSize(new java.awt.Dimension(960, 960));
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setMaximumSize(new java.awt.Dimension(960, 960));
        statusPanel.setName("statusPanel"); // NOI18N
        statusPanel.setPreferredSize(new java.awt.Dimension(400, 25));

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setMaximumSize(new java.awt.Dimension(960, 14));
        progressBar.setName("progressBar"); // NOI18N

        gameModeBtn.setAction(actionMap.get("gameModeChange")); // NOI18N
        gameModeBtn.setIcon(resourceMap.getIcon("gameModeBtn.icon")); // NOI18N
        gameModeBtn.setText(resourceMap.getString("gameModeBtn.text")); // NOI18N
        gameModeBtn.setName("gameModeBtn"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(statusAnimationLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(statusMessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 950, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addContainerGap(709, Short.MAX_VALUE)
                        .addComponent(gameModeBtn)
                        .addGap(18, 18, 18)
                        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(gameModeBtn)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(statusAnimationLabel))
                .addContainerGap())
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents



    /**
     * author: MA
     * Diese Funktion ist nur im Spiel Modus verfügbar.
     * Färbt den Hintergrund der Felder, in denen falsche Zahlen stehen, rot ein.
     */
    @Action
    public void jMenuItemFehlerAnzeigen()
    {

        boolean[][] temp = new boolean[9][9];
        temp = MrSudokuApp.getApplication().fehlerAnzeigen();

        for (int i = 0; i < temp.length; i++)
        {
            for (int j = 0; j < temp.length; j++)
            {

                if (MrSudokuApp.getApplication().getArrEditable(i, j) == false)
                {
                    sudokuData[i][j].setEnabled(false);
                    sudokuData[i][j].setEditable(false);
                }
                else
                {
                    sudokuData[i][j].setBackground(Color.white);
                }
                
                if (temp[i][j] == false)
                {
                    sudokuData[i][j].setBackground(Color.pink);
                }
            }
        }
    }

    /**
     * author: MA
     * Diese Funktion ist nur im Spiel Modus verfügbar.
     * Für ein zufälliges, leeres Feld in der GUI wird die richtige Zahl angezeigt.
     * @param evt
     */
    @Action
    public void jMenuItemLoesungshinweis(java.awt.event.ActionEvent evt)
    {
        int[] temp = new int[3];
        temp = MrSudokuApp.getApplication().loesungshinweis();
        sudokuData[temp[0]][temp[1]].setText(Integer.toString(temp[2]));
    }

    /**
     * author: MA
     * Diese Funktion ist nur im Spiel Modus verfügbar.
     * Zeigt das komplett gelöste Sudoku in der GUI an. In den Feldern, in denen der Benutzer falsche Zahlen eingetragen hat oder die noch leer waren,
     * unterscheiden sich die Zahlen farblich von den anderen Zahlen.
     */
    @Action
    public void solveSudoku()
    {


        int[][] temp = new int[9][9];
        temp = MrSudokuApp.getApplication().getArrSolved();

        for (int i = 0; i < sudokuData.length; i++)
        {
            for (int j = 0; j < sudokuData.length; j++)
            {
                if (!sudokuData[i][j].getText().equals(Integer.toString(temp[i][j])))
                {
                    sudokuData[i][j].setText(Integer.toString(temp[i][j]));
                    sudokuData[i][j].setForeground(Color.BLUE);
                }
            }
        }
        setDefaultBackground();
        setDisabledAndNotEditable();
    }

  
    /**
     * author: MA
     * Setzt den Hintergrund Felder der GUI auf die Standard Hintergrundfarbe.
     */
    public void setDefaultBackground()
    {
        for (int i = 0; i < sudokuData.length; i++)
            for (int j = 0; j < sudokuData.length; j++)
                sudokuData[i][j].setBackground(Color.white);
    }
    /**
     * author: MA
     * Setzt den Vordergrund aller Felder der GUI auf die Standard Vordergrundfarbe.
     */
    public void setDefaultForeground()
    {
        for (int i = 0; i < sudokuData.length; i++)
            for (int j = 0; j < sudokuData.length; j++)
                sudokuData[i][j].setForeground(Color.darkGray);
    }
    /**
     * author: MA
     * Setzt für Felder der GUI enabled und editable auf true.
     */
    public void setAllEnabledAndEditable(boolean state)
    {
        for (int i = 0; i < sudokuData.length; i++)
            for (int j = 0; j < sudokuData.length; j++)
            {
                sudokuData[i][j].setEnabled(state);
                sudokuData[i][j].setEditable(state);
            }
    }
    /**
     * author: MA
     * Setzt für die Felder der GUI, die beim Wechsel vom Editier- in den Spielmodus gefüllt waren, editable und enabled auf false.
     */
    public void setDisabledAndNotEditable()
    {
        for (int i = 0; i < sudokuData.length; i++)
            for (int j = 0; j < sudokuData.length; j++)
                if (MrSudokuApp.getApplication().getArrEditable(i, j) == false)
                {
                    sudokuData[i][j].setEditable(false);
                    sudokuData[i][j].setEnabled(false);
                }
    }


    /**
     * 
     */
    @Action
    public void openSudoku()
    {
        

        if (openBox == null)
        {
            JFrame mainFrame = MrSudokuApp.getApplication().getMainFrame();
            openBox = new MrSudokuOpenBox(mainFrame, true);
            openBox.setLocationRelativeTo(mainFrame);
        }
        MrSudokuApp.getApplication().show(openBox);

        // MrSudokuApp.printArr(app.getArr());

        setSudokuNumbers(MrSudokuApp.getApplication().getArr());

    }

    /**
     * 
     */
    @Action
    public void saveSudoku()
    {

        // aktualiesiere den array mit zahlen und rufe speicherdialog auf 

        MrSudokuApp.getApplication().setArr(this.getSudokuNumbers());

        if (saveBox == null)
        {
            JFrame mainFrame = MrSudokuApp.getApplication().getMainFrame();
            saveBox = new MrSudokuSaveBox(mainFrame, true);
            saveBox.setLocationRelativeTo(mainFrame);
        }
        MrSudokuApp.getApplication().show(saveBox);



    }

    /**
     * PW action für die MrSudokuManual
     */
    @Action
    public void manualSudoku()
    {
        if (manualBox == null)
        {
            JFrame mainFrame = MrSudokuApp.getApplication().getMainFrame();
            manualBox = new MrSudokuManual(mainFrame, true);
            manualBox.setLocationRelativeTo(mainFrame);
        }
        MrSudokuApp.getApplication().show(manualBox);
    }

    /**
     * 
     * @return 
     */
    public int[][] getSudokuNumbers()
    {

        int[][] arr = new int[9][9];

        for (int i = 0; i < 9; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                arr[i][j] = sudokuData[i][j].getNumber();
            }
        }

        return arr;
    }

    /**
     * 
     * @param nums 
     */
    private void setSudokuNumbers(int[][] nums)
    {
        //MrSudokuApp.printArr(nums);

        for (int i = 0; i < 9; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                sudokuData[i][j].setNumber(nums[i][j]);
            }
        }
    }

    @Action
    public void newSudoku()
    {

        

        int[][] empty = new int[9][9];
        setSudokuNumbers(empty);

        MrSudokuApp.getApplication().setActiveFile(new File("temp.sdf"));

    }

    @Action
    public void saveActive()
    {
        File active = MrSudokuApp.getApplication().getActiveFile();
        if (active.getName().equals("temp.sdf"))
        {
            saveSudoku();
        } else
        {
            MrSudokuApp.getApplication().save(MrSudokuApp.getApplication().getActiveFile());

        }


    }

    @Action
    public void gameModeChange()
    {



        boolean currentGameMode = MrSudokuApp.getApplication().getGameMode();

        if (currentGameMode) // spielmodus
        {

            // in editmode wechseln, boxen und menues enablen , buttontext und -icon ändern
            MrSudokuApp.getApplication().setGameMode(!currentGameMode);

            for (int i = 0; i < 9; ++i)
            {
                for (int j = 0; j < 9; ++j)
                {
                    sudokuData[i][j].setEnabled(true);
                    sudokuData[i][j].setEditable(true);
                    MrSudokuApp.getApplication().setArrEditable(true, i, j);
                }
            }
            statusMessageLabel.setText("");

            gameModeBtn.setText("Spielen");
            gameModeBtn.setIcon(rMap.getIcon("gameModePlay.icon"));
            editMenu.setEnabled(false);
            setDefaultBackground();
            setDefaultForeground();

        } else // editmodus
        {

            try
            {

                MrSudokuApp.getApplication().setArr(this.getSudokuNumbers());
                //checken ob sudoku valide ist
                MrSudokuApp.getApplication().solveInitialize();


                // in spielmode wechseln, boxen und menues disablen , buttontext und -icon ändern
                MrSudokuApp.getApplication().setGameMode(!currentGameMode);

                for (int i = 0; i < 9; ++i)
                {
                    for (int j = 0; j < 9; ++j)
                    {
                        MrSudokuDigitBox box = sudokuData[i][j];
                        if (box.getNumber() != 0)
                        {
                            box.setEnabled(false);
                            box.setEditable(false);
                            MrSudokuApp.getApplication().setArrEditable(false, i, j);
                        }
                    }
                }
                gameModeBtn.setText("Editieren");
                gameModeBtn.setIcon(rMap.getIcon("gameModeEdit.icon"));
                editMenu.setEnabled(true);

            } catch (SolveException se)
            {
                // warnung ausgeben
                if (warningBox == null)
                {
                    JFrame mainFrame = MrSudokuApp.getApplication().getMainFrame();
                    warningBox = new MrSudokuWarningBox(mainFrame, true, "Leider ist Ihr eingetragenes Sudoku nicht valide, bitte überprüfen Sie die Werte und versuchen Sie es erneut.");
                    warningBox.setLocationRelativeTo(mainFrame);
                }
                MrSudokuApp.getApplication().show(warningBox);


            }


        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem errorMenuItem;
    private javax.swing.JToggleButton gameModeBtn;
    private javax.swing.JMenuItem hintMenuItem;
    private javax.swing.JMenuItem loesenMenuItem;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem manualMenuItem;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JMenuItem saveActiveMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    private JDialog saveBox;
    private JDialog openBox;
    private JDialog manualBox;
    private JDialog warningBox;
    private MrSudokuDigitBox[][] sudokuData;
    private ResourceMap rMap;
}
